import React from 'react';
import { Alert, Skeleton } from 'antd';
import { useRequest, defaultFormatResult } from '@/hooks';
import {
    queryEmployeeOnJobInfo,
    queryEmployeeStatusInfoVO,
    queryEmployeeDutyList
} from '@/pages/Organization/EmployeeManage/services';
import PositionSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/PositionSection';
import EmployeeStatusSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/EmployeeStatusSection';
import DutySection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/DutySection';

/**
 * 员工详情 - 在职信息
 */
const PositionTabContent = ({ employeeCode, dutyCode }) => {
    // 在职信息数据
    const { data: positionData, loading: positionLoading, error: positionError } = useRequest(
        () => queryEmployeeOnJobInfo({ employeeCode, dutyCode }),
        {
            cacheKey: `employee-position-${employeeCode}-${dutyCode}`,
            refreshDeps: [employeeCode, dutyCode],
            ...defaultFormatResult
        }
    );
    // 员工状态数据
    const { data: employeeStatusData, loading: employeeLoading, error: employeeStatusError } = useRequest(
        () => queryEmployeeStatusInfoVO({ employeeCode }),
        {
            cacheKey: `employee-status-${employeeCode}`,
            refreshDeps: [employeeCode],
            ...defaultFormatResult
        }
    );
    // 岗位兼任数据
    const { data: dutyData, loading: dutyLoading, error: dutyError } = useRequest(
        () => queryEmployeeDutyList({ employeeCode }),
        {
            cacheKey: `employee-duty-list-${employeeCode}`,
            refreshDeps: [employeeCode],
            ...defaultFormatResult
        }
    );

    // 错误处理
    if (positionError || employeeStatusError || dutyError) {
        return (
            <Alert
                type="error"
                message={positionError?.msg || employeeStatusError?.msg || dutyError?.msg || '数据加载错误'}
            />
        );
    }

    // render
    return (
        <>
            <Skeleton loading={!positionData || positionLoading} active>
                {/* 在职信息 */}
                <PositionSection formData={positionData} />
            </Skeleton>
            <Skeleton loading={!employeeStatusData || employeeLoading} active>
                {/* 员工状态 */}
                <EmployeeStatusSection formData={employeeStatusData} />
            </Skeleton>
            <Skeleton loading={!dutyData || dutyLoading} active>
                {/* 岗位兼任 */}
                <DutySection formData={dutyData} />
            </Skeleton>
        </>
    );
};

export default PositionTabContent;
